function[]=simulbivar(akce)
%SIMULBIVAR  simulation of the bivariate normal mixture density
%
%  SIMULBIVAR starts a window with menu for simulation of the bivariate
%  normal mixture density
%
% (C) Jan Kolacek, Masaryk University (Czech Republic)

if nargin<1
    
    if ~exist('v.mat','file')
        inic=[];save v.mat inic;
    end
    
     callbackStr=['set(gcf,''CloseRequestFcn'',''closereq'');',...
         'G=get(gcf,''UserData'');close(findobj(''tag'',''data''));',...
         'close(findobj(''tag'',''dens''));clear;load puvprom;',...
         'delete(gcf);'];
    
    crStr= 'save puvprom;';

    simfig=figure( ...
        'Visible','on', ...
        'Name','SIMULATION OF NORMAL MIXTURE', ...
        'CloseRequestFcn',callbackStr,...
        'Units','Normalized',...
        'Tag','kclos',...
        'CreateFcn',crStr,...
        'NumberTitle','off');
    
    axes('Units','normalized', ...
        'Position',[0.08 0.08 0.62 0.67]);
    
    top=.85;
    fsz=0.31;  %FontSize
    
    uicontrol( ...
        'Style','text', ...
        'Units','normalized', ...
        'FontUnits','normalized',...
        'Position',[.08,top+0.09,.2,.05], ...
        'HorizontalAlignment','left',...
        'BackgroundColor',[0.8 0.8 0.8], ...
        'ForegroundColor',[0 0 0], ...
        'FontSize',fsz*1.8,...
        'FontWeight','bold',...
        'String','Set # of densities:');
    
    call1='G=get(gcf,''userdata'');hL=G.hndlList;ind=get(hL(1),''Value'');';
    call2='for i = 1:6*(ind-1) set(hL(i+4),''Visible'',''on''); end;';
    call3=['switch ind, case 1, set(hL(4),''String'',''1'');p1=1;',...
        'case 2, set([hL(4),hL(10)],''String'',''1/2'');p1=1/2;p2=1/2;',...
        'case 3, set([hL(4),hL(10),hL(16)],''String'',''1/3'');p1=1/3;p2=1/3;p3=1/3;',...
        'case 4, set([hL(4),hL(10),hL(16),hL(22)],''String'',''1/4'');p1=1/4;p2=1/4;p3=1/4;p4=1/4;',...
        'case 5, set([hL(4),hL(10),hL(16),hL(22),hL(28)],''String'',''1/5'');p1=1/5;p2=1/5;p3=1/5;p4=1/5;p5=1/5;',...
        'end;'];
    call4='for i = 6*(ind-1)+1:24 set(hL(i+4),''Visible'',''off''); end;';
    rHndl=uicontrol( ...
        'Style','popup', ...
        'Units','normalized', ...
        'FontUnits','normalized',...
        'Position',[.27,top+.12,.05,.025], ...
        'BackgroundColor',[0.6 0 0],...
        'ForegroundColor',[1 1 1],...
        'String',num2str((1:5)'), ...
        'FontSize',2*fsz+.3,...
        'FontWeight','bold',...
        'Value',1,...
        'Callback',[call1,call2,call3,call4]);
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    % Set1
    uicontrol( ...
        'Style','text', ...
        'Units','normalized', ...
        'FontUnits','normalized',...
        'Position',[.08+.16*0,top+0.03,.05,.05], ...
        'HorizontalAlignment','right',...
        'BackgroundColor',[0.8 0.8 0.8], ...
        'ForegroundColor',[0 0 0], ...
        'FontSize',2*fsz,...
        'String','mu1:');
    
    call='G=get(gcf,''userdata'');mu1=str2num(get(G.hndlList(2),''String''));';
    mu1Hndl=uicontrol( ...
        'Style','edit', ...
        'Units','normalized', ...
        'FontUnits','normalized',...
        'Position',[.14+.16*0,top+.04,.09,.05], ...
        'String','[0 0]', ...
        'HorizontalAlignment','left',...
        'FontSize',2*fsz,...
        'CreateFcn','mu1=[0 0];',...
        'Callback',call);
    
    uicontrol( ...
        'Style','text', ...
        'Units','normalized', ...
        'FontUnits','normalized',...
        'Position',[.08+.16*0,top-0.03,.05,.05], ...
        'HorizontalAlignment','right',...
        'BackgroundColor',[0.8 0.8 0.8], ...
        'ForegroundColor',[0 0 0], ...
        'FontSize',2*fsz,...
        'String','S1:');
    
    call='G=get(gcf,''userdata'');S1=str2num(get(G.hndlList(3),''String''));';
    sig1Hndl=uicontrol( ...
        'Style','edit', ...
        'Units','normalized', ...
        'FontUnits','normalized',...
        'Position',[.14+.16*0,top-0.02,.09,.05], ...
        'String','[1 0;0 1]', ...
        'HorizontalAlignment','left',...
        'FontSize',2*fsz,...
        'CreateFcn','S1=[1 0;0 1];',...
        'Callback',call);
    
    uicontrol( ...
        'Style','text', ...
        'Units','normalized', ...
        'FontUnits','normalized',...
        'Position',[.08+.16*0,top-0.09,.05,.05], ...
        'HorizontalAlignment','right',...
        'BackgroundColor',[0.8 0.8 0.8], ...
        'ForegroundColor',[0 0 0], ...
        'FontSize',2*fsz,...
        'String','p1:');
    
    call='G=get(gcf,''userdata'');p1=str2num(get(G.hndlList(4),''String''));';
    p1Hndl=uicontrol( ...
        'Style','edit', ...
        'Units','normalized', ...
        'FontUnits','normalized',...
        'Position',[.14+.16*0,top-0.08,.09,.05], ...
        'String','1', ...
        'HorizontalAlignment','left',...
        'FontSize',2*fsz,...
        'CreateFcn','p1=1;',...
        'Callback',call);
    
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    % Set2
    tmu2Hndl=uicontrol( ...
        'Style','text', ...
        'Visible','off', ...
        'Units','normalized', ...
        'FontUnits','normalized',...
        'Position',[.08+.16*1,top+0.03,.05,.05], ...
        'HorizontalAlignment','right',...
        'BackgroundColor',[0.8 0.8 0.8], ...
        'ForegroundColor',[0 0 0], ...
        'FontSize',2*fsz,...
        'String','mu2:');
    
    call='G=get(gcf,''userdata'');mu2=str2num(get(G.hndlList(6),''String''));';
    mu2Hndl=uicontrol( ...
        'Style','edit', ...
        'Visible','off', ...
        'Units','normalized', ...
        'FontUnits','normalized',...
        'Position',[.14+.16*1,top+.04,.09,.05], ...
        'String','[1 0]', ...
        'HorizontalAlignment','left',...
        'FontSize',2*fsz,...
        'CreateFcn','mu2=[1 0];',...
        'Callback',call);
    
    tsig2Hndl=uicontrol( ...
        'Style','text', ...
        'Visible','off', ...
        'Units','normalized', ...
        'FontUnits','normalized',...
        'Position',[.08+.16*1,top-0.03,.05,.05], ...
        'HorizontalAlignment','right',...
        'BackgroundColor',[0.8 0.8 0.8], ...
        'ForegroundColor',[0 0 0], ...
        'FontSize',2*fsz,...
        'String','S2:');
    
    call='G=get(gcf,''userdata'');S2=str2num(get(G.hndlList(8),''String''));';
    sig2Hndl=uicontrol( ...
        'Style','edit', ...
        'Visible','off', ...
        'Units','normalized', ...
        'FontUnits','normalized',...
        'Position',[.14+.16*1,top-0.02,.09,.05], ...
        'String','[1 0;0 1]', ...
        'HorizontalAlignment','left',...
        'FontSize',2*fsz,...
        'CreateFcn','S2=[1 0;0 1];',...
        'Callback',call);
    
    tp2Hndl=uicontrol( ...
        'Style','text', ...
        'Visible','off', ...
        'Units','normalized', ...
        'FontUnits','normalized',...
        'Position',[.08+.16*1,top-0.09,.05,.05], ...
        'HorizontalAlignment','right',...
        'BackgroundColor',[0.8 0.8 0.8], ...
        'ForegroundColor',[0 0 0], ...
        'FontSize',2*fsz,...
        'String','p2:');
    
    call='G=get(gcf,''userdata'');p2=str2num(get(G.hndlList(10),''String''));';
    p2Hndl=uicontrol( ...
        'Style','edit', ...
        'Visible','off', ...
        'Units','normalized', ...
        'FontUnits','normalized',...
        'Position',[.14+.16*1,top-0.08,.09,.05], ...
        'String','1/2', ...
        'HorizontalAlignment','left',...
        'FontSize',2*fsz,...
        'Callback',call);
    
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    % Set3
    tmu3Hndl=uicontrol( ...
        'Style','text', ...
        'Visible','off', ...
        'Units','normalized', ...
        'FontUnits','normalized',...
        'Position',[.08+.16*2,top+0.03,.05,.05], ...
        'HorizontalAlignment','right',...
        'BackgroundColor',[0.8 0.8 0.8], ...
        'ForegroundColor',[0 0 0], ...
        'FontSize',2*fsz,...
        'String','mu3:');
    
    call='G=get(gcf,''userdata'');mu3=str2num(get(G.hndlList(12),''String''));';
    mu3Hndl=uicontrol( ...
        'Style','edit', ...
        'Visible','off', ...
        'Units','normalized', ...
        'FontUnits','normalized',...
        'Position',[.14+.16*2,top+.04,.09,.05], ...
        'String','[0 1]', ...
        'HorizontalAlignment','left',...
        'FontSize',2*fsz,...
        'CreateFcn','mu3=[0 1];',...
        'Callback',call);
    
    tsig3Hndl=uicontrol( ...
        'Style','text', ...
        'Visible','off', ...
        'Units','normalized', ...
        'FontUnits','normalized',...
        'Position',[.08+.16*2,top-0.03,.05,.05], ...
        'HorizontalAlignment','right',...
        'BackgroundColor',[0.8 0.8 0.8], ...
        'ForegroundColor',[0 0 0], ...
        'FontSize',2*fsz,...
        'String','S3:');
    
    call='G=get(gcf,''userdata'');S3=str2num(get(G.hndlList(14),''String''));';
    sig3Hndl=uicontrol( ...
        'Style','edit', ...
        'Visible','off', ...
        'Units','normalized', ...
        'FontUnits','normalized',...
        'Position',[.14+.16*2,top-0.02,.09,.05], ...
        'String','[1 0;0 1]', ...
        'HorizontalAlignment','left',...
        'FontSize',2*fsz,...
        'CreateFcn','S3=[1 0;0 1];',...
        'Callback',call);
    
    tp3Hndl=uicontrol( ...
        'Style','text', ...
        'Visible','off', ...
        'Units','normalized', ...
        'FontUnits','normalized',...
        'Position',[.08+.16*2,top-0.09,.05,.05], ...
        'HorizontalAlignment','right',...
        'BackgroundColor',[0.8 0.8 0.8], ...
        'ForegroundColor',[0 0 0], ...
        'FontSize',2*fsz,...
        'String','p3:');
    
    call='G=get(gcf,''userdata'');p3=str2num(get(G.hndlList(16),''String''));';
    p3Hndl=uicontrol( ...
        'Style','edit', ...
        'Visible','off', ...
        'Units','normalized', ...
        'FontUnits','normalized',...
        'Position',[.14+.16*2,top-0.08,.09,.05], ...
        'String','1/3', ...
        'HorizontalAlignment','left',...
        'FontSize',2*fsz,...
        'Callback',call);
    
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    % Set4
    tmu4Hndl=uicontrol( ...
        'Style','text', ...
        'Visible','off', ...
        'Units','normalized', ...
        'FontUnits','normalized',...
        'Position',[.08+.16*3,top+0.03,.05,.05], ...
        'HorizontalAlignment','right',...
        'BackgroundColor',[0.8 0.8 0.8], ...
        'ForegroundColor',[0 0 0], ...
        'FontSize',2*fsz,...
        'String','mu4:');
    
    call='G=get(gcf,''userdata'');mu4=str2num(get(G.hndlList(18),''String''));';
    mu4Hndl=uicontrol( ...
        'Style','edit', ...
        'Visible','off', ...
        'Units','normalized', ...
        'FontUnits','normalized',...
        'Position',[.14+.16*3,top+.04,.09,.05], ...
        'String','[-1 0]', ...
        'HorizontalAlignment','left',...
        'FontSize',2*fsz,...
        'CreateFcn','mu4=[-1 0];',...
        'Callback',call);
    
    tsig4Hndl=uicontrol( ...
        'Style','text', ...
        'Visible','off', ...
        'Units','normalized', ...
        'FontUnits','normalized',...
        'Position',[.08+.16*3,top-0.03,.05,.05], ...
        'HorizontalAlignment','right',...
        'BackgroundColor',[0.8 0.8 0.8], ...
        'ForegroundColor',[0 0 0], ...
        'FontSize',2*fsz,...
        'String','S4:');
    
    call='G=get(gcf,''userdata'');S4=str2num(get(G.hndlList(20),''String''));';
    sig4Hndl=uicontrol( ...
        'Style','edit', ...
        'Visible','off', ...
        'Units','normalized', ...
        'FontUnits','normalized',...
        'Position',[.14+.16*3,top-0.02,.09,.05], ...
        'String','[1 0;0 1]', ...
        'HorizontalAlignment','left',...
        'FontSize',2*fsz,...
        'CreateFcn','S4=[1 0;0 1];',...
        'Callback',call);
    
    tp4Hndl=uicontrol( ...
        'Style','text', ...
        'Visible','off', ...
        'Units','normalized', ...
        'FontUnits','normalized',...
        'Position',[.08+.16*3,top-0.09,.05,.05], ...
        'HorizontalAlignment','right',...
        'BackgroundColor',[0.8 0.8 0.8], ...
        'ForegroundColor',[0 0 0], ...
        'FontSize',2*fsz,...
        'String','p4:');
    
    call='G=get(gcf,''userdata'');p4=str2num(get(G.hndlList(22),''String''));';
    p4Hndl=uicontrol( ...
        'Style','edit', ...
        'Visible','off', ...
        'Units','normalized', ...
        'FontUnits','normalized',...
        'Position',[.14+.16*3,top-0.08,.09,.05], ...
        'String','1/4', ...
        'HorizontalAlignment','left',...
        'FontSize',2*fsz,...
        'Callback',call);
    
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    % Set5
    tmu5Hndl=uicontrol( ...
        'Style','text', ...
        'Visible','off', ...
        'Units','normalized', ...
        'FontUnits','normalized',...
        'Position',[.08+.16*4,top+0.03,.05,.05], ...
        'HorizontalAlignment','right',...
        'BackgroundColor',[0.8 0.8 0.8], ...
        'ForegroundColor',[0 0 0], ...
        'FontSize',2*fsz,...
        'String','mu5:');
    
    call='G=get(gcf,''userdata'');mu5=str2num(get(G.hndlList(24),''String''));';
    mu5Hndl=uicontrol( ...
        'Style','edit', ...
        'Visible','off', ...
        'Units','normalized', ...
        'FontUnits','normalized',...
        'Position',[.14+.16*4,top+.04,.09,.05], ...
        'String','[0 -1]', ...
        'HorizontalAlignment','left',...
        'FontSize',2*fsz,...
        'CreateFcn','mu5=[0 -1];',...
        'Callback',call);
    
    tsig5Hndl=uicontrol( ...
        'Style','text', ...
        'Visible','off', ...
        'Units','normalized', ...
        'FontUnits','normalized',...
        'Position',[.08+.16*4,top-0.03,.05,.05], ...
        'HorizontalAlignment','right',...
        'BackgroundColor',[0.8 0.8 0.8], ...
        'ForegroundColor',[0 0 0], ...
        'FontSize',2*fsz,...
        'String','S5:');
    
    call='G=get(gcf,''userdata'');S5=str2num(get(G.hndlList(26),''String''));';
    sig5Hndl=uicontrol( ...
        'Style','edit', ...
        'Visible','off', ...
        'Units','normalized', ...
        'FontUnits','normalized',...
        'Position',[.14+.16*4,top-0.02,.09,.05], ...
        'String','[1 0;0 1]', ...
        'HorizontalAlignment','left',...
        'FontSize',2*fsz,...
        'CreateFcn','S5=[1 0;0 1];',...
        'Callback',call);
    
    tp5Hndl=uicontrol( ...
        'Style','text', ...
        'Visible','off', ...
        'Units','normalized', ...
        'FontUnits','normalized',...
        'Position',[.08+.16*4,top-0.09,.05,.05], ...
        'HorizontalAlignment','right',...
        'BackgroundColor',[0.8 0.8 0.8], ...
        'ForegroundColor',[0 0 0], ...
        'FontSize',2*fsz,...
        'String','p5:');
    
    call='G=get(gcf,''userdata'');p5=str2num(get(G.hndlList(28),''String''));';
    p5Hndl=uicontrol( ...
        'Style','edit', ...
        'Visible','off', ...
        'Units','normalized', ...
        'FontUnits','normalized',...
        'Position',[.14+.16*4,top-0.08,.09,.05], ...
        'String','1/5', ...
        'HorizontalAlignment','left',...
        'FontSize',2*fsz,...
        'Callback',call);
    
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%The PRINT button
    
call1=['G=get(gcf,''userdata'');fig=figure(''Units'',''normalized'',',...
    '''NumberTitle'',''off'',''Name'',''DATA PLOT'',''Position'',[0.1059 0.1655 0.7700 0.6898]);',...
    'figure(G.simfig);copyobj(get(gcf,''CurrentAxes''),fig);figure(fig);',...
    'set(gca,''Position'',[0.08 0.08 0.84 0.84]);'];
call2=['hL=G.hndlList;clbr=get(hL(34),''Value'');',...
    'if clbr==1, colorbar; end;set(gca,''DataAspectRatioMode'',''manual'');'];
printHndl=uicontrol( ...
        'Style','push', ...
        'Enable','off',...
        'Units','normalized', ...
        'FontUnits','normalized',...
        'Tooltipstring','Print actual plot to a new figure',...
        'Position',[.89,top+.04,.05,.05], ...
        'String','P', ...
        'HorizontalAlignment','right',...
        'FontSize',2*fsz,...
        'CreateFcn','n=100;',...
        'Callback',[call1,call2]);

    
    %The N button    
    uicontrol( ...
        'Style','text', ...
        'Units','normalized', ...
        'FontUnits','normalized',...
        'Position',[.75,top-.2,.14,.1], ...
        'HorizontalAlignment','left',...
        'BackgroundColor',[0.8 0.8 0.8], ...
        'ForegroundColor',[0 0 0], ...
        'FontSize',fsz,...
        'FontWeight','bold',...
        'String','Set the sample size:');
    
    call='G=get(gcf,''userdata'');n=str2num(get(G.hndlList(32),''String''));';
    nHndl=uicontrol( ...
        'Style','edit', ...
        'Units','normalized', ...
        'FontUnits','normalized',...
        'Position',[.89,top-.18,.05,.05], ...
        'String','100', ...
        'HorizontalAlignment','right',...
        'FontSize',2*fsz,...
        'CreateFcn','n=100;',...
        'Callback',call);
    
    %The SIMULATE button
    simulHndl=uicontrol( ...
        'Style','push', ...
        'Enable','on',...
        'Units','normalized', ...
        'FontUnits','normalized',...
        'Position',[0.8,top-.32,.1,0.1], ...
        'String','Simulate', ...
        'Tooltipstring','Generate a random sample',...
        'FontSize',fsz,...
        'Callback','simulbivar(''gener'')');
    
    uicontrol( ...
        'Style','text', ...
        'Units','normalized', ...
        'FontUnits','normalized',...
        'Position',[.75,top-.4,.2,.05], ...
        'HorizontalAlignment','left',...
        'BackgroundColor',[0.8 0.8 0.8], ...
        'ForegroundColor',[0 0 0], ...
        'FontSize',2*fsz,...
        'FontWeight','bold',...
        'String','Type of view:');
    
    c1='G=get(gcf,''UserData'');hL=G.hndlList;set(hL(34),''value'',0);';
    c2='simulbivar(''dens'');';
    rad1Hndl=uicontrol( ...
        'Style','radiobutton', ...
        'Enable','off',...
        'Units','normalized', ...
        'FontUnits','normalized',...
        'Position',[.75,top-.45,.2,.05], ...
        'HorizontalAlignment','left',...
        'FontSize',2*fsz,...
        'BackgroundColor',[0.8 0.8 0.8], ...
        'ForegroundColor',[0 0 0], ...
        'Callback',[c1,c2],...
        'String','Density function');
    
    c1='G=get(gcf,''UserData'');hL=G.hndlList;set(hL(33),''value'',0);';
    c2='simulbivar(''data'');';
    rad2Hndl=uicontrol( ...
        'Style','radiobutton', ...
        'Units','normalized', ...
        'Enable','off',...
        'FontUnits','normalized',...
        'Position',[.75,top-.5,.2,.05], ...
        'HorizontalAlignment','left',...
        'FontSize',2*fsz,...
        'BackgroundColor',[0.8 0.8 0.8], ...
        'ForegroundColor',[0 0 0], ...
        'Value',1,...
        'Callback',[c1,c2],...
        'String','Data & contour');
    
    %
    % The SAVE text
    uicontrol( ...
        'Style','text', ...
        'Units','normalized', ...
        'FontUnits','normalized',...
        'HorizontalAlignment','left',...
        'Position',[.8,.26,.2,.05], ...
        'BackgroundColor',[0.8 0.8 0.8], ...
        'FontSize',2*fsz,...
        'FontWeight','bold',...
        'String','Save Data:');
    
    % The VAR button
    call=['checkLabels = {''Save sample to variable named:'','...
        '''Save density function to variable named:''};',...
        'varNames = {''X'',''f''};',...
        'G=get(gcf,''UserData'');X=G.X;f=G.f;',...
        'items = {X,f};save ppne X f;clear X f;',...
        'save ppp;save pp checkLabels varNames items;clear;load pp;delete pp.mat;',...
        '[hdialog,ok_pressed]=export2wsdlg(checkLabels,varNames,items,''Save Data'');'...
        'if ok_pressed G=get(gcf,''userdata'');set(G.hndlList(31),''enable'',''on'');',...
        'clear G hdialog ok_pressed checkLabels varNames items;varNames=who;',...
        'save v varNames -append;save(''puvprom'',varNames{:},''-append'');',...
        'else load ppne;end;load ppp;delete ppp.mat;delete ppne.mat;'];
    varHndl=uicontrol( ...
        'Style','push', ...
        'Enable','off',...
        'Units','normalized', ...
        'FontUnits','normalized',...
        'Position',[0.8,.21,.05,0.05], ...
        'String','var', ...
        'FontSize',2*fsz,...
        'Callback',call);
    
    % The FILE button
    labelStr='file';
    call='load v; uisave(varNames,''data_sim'')';
    fileHndl=uicontrol( ...
        'Style','push', ...
        'Enable','off',...
        'Units','normalized', ...
        'FontUnits','normalized',...
        'Position',[0.85,.21,.05,0.05], ...
        'String',labelStr, ...
        'FontSize',2*fsz,...
        'Callback',call);
    
    % The CLOSE button
    labelStr='Close';
    uicontrol( ...
        'Style','push', ...
        'Units','normalized', ...
        'FontUnits','normalized',...
        'Position',[0.84,.07,.1,0.1], ...
        'String',labelStr, ...
        'FontSize',fsz,...
        'Callback',callbackStr);
    
    osy=[0 1 0 1];
    G.hndlList=[rHndl,mu1Hndl,sig1Hndl,p1Hndl,tmu2Hndl,mu2Hndl,tsig2Hndl,sig2Hndl,tp2Hndl,p2Hndl,...
        tmu3Hndl,mu3Hndl,tsig3Hndl,sig3Hndl,tp3Hndl,p3Hndl,...
        tmu4Hndl,mu4Hndl,tsig4Hndl,sig4Hndl,tp4Hndl,p4Hndl,...
        tmu5Hndl,mu5Hndl,tsig5Hndl,sig5Hndl,tp5Hndl,p5Hndl,...
        simulHndl,varHndl,fileHndl,nHndl,rad1Hndl,rad2Hndl,printHndl,osy];
    G.simfig=simfig;
    set(gcf,'UserData',G);
    %set(C,'Position',[0 0.0347 1.0000 0.9190]);
    set(simfig,'Position',[0.1059 0.1655 0.7700 0.6898]);
    %set(C,'Position',[1 1 .97 .9].*scrsz+[.01 .012 0 0]);
    
else switch akce
        case 'gener'
            G=get(gcf,'userdata');
            close(findobj('tag','data'))
            close(findobj('tag','dens'))
            list=G.hndlList;
            k=get(list(1),'value');
            set(list(30),'Enable','on');
            set(list(35),'Enable','on');
            set(list(33),'Enable','on','Value',0);
            set(list(34),'Enable','on','Value',1);
            
            mu=[];sigma=[];p=[];
            for i=1:k
                mu(i,:)=evalin('base',['mu',num2str(i)]);
                sigma(:,:,i)=evalin('base',['S',num2str(i)]);
                p(i)=evalin('base',['p',num2str(i)]);
            end
            obj = gmdistribution(mu,sigma,p);
            f=[];
            for i=1:length(p)
                C=sigma(:,:,i);
                detC=det(C);
                C=inv(C);
                c11=num2str(C(1,1));
                c12=num2str(C(1,2));
                c22=num2str(C(2,2));
                mu1=num2str(mu(i,1));
                mu2=num2str(mu(i,2));
                f=[f,'+',num2str(p(i)),'*1/(2*pi*sqrt(',num2str(detC),'))*exp(-0.5*(',c11,'*(x-(',mu1,'))^2+2*(',c12,')*(x-(',mu1,'))*(y-(',mu2,'))+',c22,'*(y-((',mu2,')))^2))'];
            end
            n=evalin('base','n');
            X=random(obj,n)';
            G.X=X;
            G.obj=obj;
            G.f=f;
            G.mu=mu;
            G.sigma=sigma;
            G.w=p;
            X_def_for_me=X;
            f_def_for_me=f;
            save puvprom.mat X_def_for_me f_def_for_me -append;

            GGG=get(findobj(0,'Tag','defprom'),'userdata');
            GGG.mu=mu;
            GGG.sigma=sigma;
            GGG.w=p;
            set(findobj(0,'Tag','defprom'),'userdata',GGG);
            GG=GGG.hndlList;
            if ~isempty(GG)
                prom=get(GG(1),'String');
                prom=pridej(prom,'X_def_for_me');
                set(GG(1),'String',prom,'Enable','on','Value',1);    
                set(GG(2),'String',f,'Enable','on');    
                prom=get(GG(3),'String');
                prom=pridej(prom,'f_def_for_me');
                set(GG(3),'String',prom,'Enable','on','Value',1);    
                set(GG(4),'Enable','on');
            end;   
            
            h1_max=std(X(1,:));
            h2_max=std(X(2,:));
            xxspec=[linspace(min(X(1,:))-h1_max,max(X(1,:))+h1_max);...
                linspace(min(X(2,:))-h2_max,max(X(2,:))+h2_max)];
            minx1=min(xxspec(1,:));
            maxx1=max(xxspec(1,:));
            minx2=min(xxspec(2,:));
            maxx2=max(xxspec(2,:));
            
            figure('visible','off','tag','dens');
            axes('Units','normalized', ...
                'Position',[0.08 0.08 0.62 0.67]);
            ezsurf(@(x,y)pdf(obj,[x y]),[minx1 maxx1],[minx2 maxx2]);
            title('');
            cax=caxis;
            figure(G.simfig);
            
            figure('visible','off','tag','data');
            axes('Units','normalized', ...
                'Position',[0.08 0.08 0.62 0.67]);
            ezcontour(@(x,y)pdf(obj,[x y]),[minx1 maxx1],[minx2 maxx2]);
            caxis(cax);
            hold on;
            plot(X(1,:),X(2,:),'x');
            title('');
            hold off;
            ez2=get(gcf,'CurrentAxes');
            figure(G.simfig);
            delete(gca);
            copyobj(ez2,G.simfig);
            hold on;
            colorbar;
            hold off;
            
            set(gcf,'UserData',G);
        case 'data'
            G=get(gcf,'UserData');
            g=findobj('tag','data');
            ez2=get(g,'CurrentAxes');
            figure(G.simfig);
            delete(gca);
            copyobj(ez2,gcf);
            hold on
            colorbar;
            hold off
        case 'dens'
            G=get(gcf,'UserData');
            g=findobj('tag','dens');
            ez1=get(g,'CurrentAxes');
            figure(G.simfig);
            delete(gca);
            copyobj(ez1,gcf);
    end
end